/**
* All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
*
* MpmMilePostTemplateService
* 里程碑及任务模板
*
* 修改纪录
* 2020-12-25 版本：1.0 lanjianwei 创建。
* @version 版本：1.0
* @author 作者：lanjianwei
* 创建日期：2020-12-25
*/
package com.topscomm.mpm.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.exception.ServiceException;
import com.topscomm.basic.util.PrimaryKeyUtil;
import com.topscomm.mpm.MpmSystemConst;
import com.topscomm.mpm.pojo.MpmMilePostTemplateEntity;
import com.topscomm.mpm.pojo.MpmProjectMilepostEntity;
import com.topscomm.mpm.pojo.MpmProjectReviewEntity;
import com.topscomm.mpm.pojo.MpmProjectTaskTemplateEntity;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.mpm.service.IMpmMilePostTemplateService;
import com.topscomm.mpm.service.impl.auto.MpmMilePostTemplateServiceAuto;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service(value = "mpmMilePostTemplateService")
@Scope(value = "singleton")
public class MpmMilePostTemplateService extends MpmMilePostTemplateServiceAuto implements IMpmMilePostTemplateService {
    @Autowired
    private MpmProjectTaskTemplateService mpmProjectTaskTemplateService;
    @Override
    protected void beforeInsert(BasicEntity entity) throws ServiceException {
        if (entity.getId() <= 0) {
            long[] arrayId = PrimaryKeyUtil.GeneIDs(entity.getTableName(), 1);
            entity.setId(arrayId[0]);
        }
    }
    /**
     * @description:
     * @author xinfushuo
     * @param
     * @return
     * @date 2021/10/18
     * @modify
     */
    @Override
    protected void updateBefore(Map<String,Object> map) {
        // 对项目级别做处理
        JSONArray levelArray = JSONArray.parseArray(map.get("projectlevel").toString());
        StringBuffer sb = new StringBuffer();
        for(int i=0;i<levelArray.size();i++){
            String level = levelArray.get(i).toString();
            if(i==levelArray.size()-1){
                sb.append(level);
            }else{
                sb.append(level + ",");
            }
        }
        map.replace("projectlevel", ConvertUtil.convertToString(sb));
    }
    /**
     * @author: houyuhang
     * @date: 2022-09-23
     * @throws ServiceException
     * @description: 删除里程碑模板时同步删除该里程碑下任务模板
     * @modify:
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    protected void deleteBefore(String whereSql) {
        super.deleteBefore(whereSql);
        // 查询删除的里程碑信息
        List<MpmMilePostTemplateEntity> mpmMilePostTemplateEntities = this.queryByWhere(whereSql);
        if (CollectionUtil.isEmpty(mpmMilePostTemplateEntities)) {
            return;
        }
        // 删除里程碑对应的任务
        String milepostid = mpmMilePostTemplateEntities.stream().map(item -> ConvertUtil.convertToString(item.getId())).collect(Collectors.joining(","));
        StringBufferProxy delsql = new StringBufferProxy();
        delsql.appendSingle(" {0} in ({1}) ", MpmProjectTaskTemplateEntity.FieldMileposttemplateid, milepostid);
        mpmProjectTaskTemplateService.deleteByWhere(delsql.toString());
    }
}
